//
//  DNExtension.swift
//  LunalerApp
//
//  Created by 露乐ios on 2018/2/7.
//  Copyright © 2018年 露乐ios. All rights reserved.
//

import Foundation
import UIKit

enum UCSScreenInch {
    case UCSScreenInch_Unknown
    case UCSScreenInch_3_5
    case UCSScreenInch_4_0
    case UCSScreenInch_4_7
    case UCSScreenInch_5_5
    case UCSScreenInch_5_8
}

extension UIScreen
{
    public func getCurrentSize(screen:UIScreen) -> CGSize
    {
        if (self.ucsIsScreen5_5Inch())
        {
            return CGSize.init(width: 414.0, height: 736.0)
        }else if(self.ucsIsScreen4_7Inch()){
            return CGSize.init(width: 375.0, height: 667.0)
        }else if(self.ucsIsScreen4_0Inch()){
            return CGSize.init(width: 320.0, height: 568.0)
        }else if(self.ucsIsScreen3_5Inch()){
            return CGSize.init(width: 320.0, height: 480.0)
        }else if(self.ucsIsScreen5_8Inch()){
            return CGSize.init(width: 375.0, height: 812.0)
        }else{
            return CGSize.zero;
        }
    }
    
    func IS_ZOOMED_IPHONE_6_PLUS() -> Bool {
        return max(UIScreen.main.bounds.size.height, UIScreen.main.bounds.size.width) == 667.0 && UIScreen.main.nativeScale < UIScreen.main.scale;
    }
    
    func IS_ZOOMED_IPHONE_6() -> Bool {
        return max(UIScreen.main.bounds.size.height, UIScreen.main.bounds.size.width) == 568.0 && UIScreen.main.nativeScale > UIScreen.main.scale;
    }
    
    func ucsCurrentScreenInch() -> UCSScreenInch {
        var currentInch = UCSScreenInch.UCSScreenInch_Unknown
        if (currentInch == UCSScreenInch.UCSScreenInch_Unknown){
            if (__CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 414.0, height: 736.0)) || __CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 736.0, height: 414.0)) || IS_ZOOMED_IPHONE_6_PLUS()){
                currentInch = UCSScreenInch.UCSScreenInch_5_5;
            }else if(__CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 375.0, height: 667.0)) || __CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 667.0, height: 375.0)) || IS_ZOOMED_IPHONE_6()){
                currentInch = UCSScreenInch.UCSScreenInch_4_7;
            }else if(__CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 320.0, height: 568.0)) || __CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 568.0, height: 320.0))){
                currentInch = UCSScreenInch.UCSScreenInch_4_0
            }else if(__CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 320.0, height: 480.0)) || __CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 480.0, height: 320.0))){
                currentInch = UCSScreenInch.UCSScreenInch_3_5
            }else if(__CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 375.0, height: 812.0)) || __CGSizeEqualToSize(UIScreen.main.bounds.size, CGSize.init(width: 812.0, height: 375.0))){
                currentInch = UCSScreenInch.UCSScreenInch_5_8
            }else{
                currentInch = UCSScreenInch.UCSScreenInch_Unknown
            }
        }
        return currentInch
    }
    
    
    func ucsIsScreen3_5Inch() -> Bool {
        if self.ucsCurrentScreenInch() == UCSScreenInch.UCSScreenInch_3_5 {
            return true
        }else{
            return false
        }
    }
    
    func ucsIsScreen4_0Inch() -> Bool {
        if self.ucsCurrentScreenInch() == UCSScreenInch.UCSScreenInch_4_0{
            return true
        }else{
            return false
        }
    }
    
    func ucsIsScreen4_7Inch() -> Bool {
        if self.ucsCurrentScreenInch() == UCSScreenInch.UCSScreenInch_4_7{
            return true
        }else{
            return false
        }
    }
    
    func ucsIsScreen5_5Inch() -> Bool {
        if self.ucsCurrentScreenInch() == UCSScreenInch.UCSScreenInch_5_5{
            return true
        }else{
            return false
        }
    }
    
    func ucsIsScreen5_8Inch() -> Bool {
        if self.ucsCurrentScreenInch() == UCSScreenInch.UCSScreenInch_5_8{
            return true
        }else{
            return false
        }
    }
}

extension NSString
{
    func UUID() -> NSString {
        let uuidStr = NSUUID().uuidString as NSString
        return uuidStr.dnStringByReplacingOccurrencesOfStringwithString(target: "-", replacement: "") as! NSString
    }
    
    func trimming() -> NSString {
        return self.trimmingCharacters(in: CharacterSet.whitespaces) as NSString
    }
    
    func md5() -> NSString {
        let cStr = self.utf8String
//        let cStr = self.cString(using: String.Encoding.utf8.rawValue);
        debugPrint("cStr info:\(String(describing: cStr))")
        let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(CC_MD5_DIGEST_LENGTH))
        
        CC_MD5(cStr, (CC_LONG)(strlen(cStr)), buffer)
        debugPrint("md5 buff info:\(buffer)")
        
        let md5String = NSMutableString()
        for i in 0..<16{
            md5String.appendFormat("%02X", buffer[i])
        }
        free(buffer)
        print("md5 string info:\(md5String)")
        return md5String.lowercased as NSString
    }
    
    func trim() -> NSString{
        return self.dnStringByReplacingOccurrencesOfStringwithString(target: " ", replacement: "") as! NSString
    }
    
    func formatterToMoney() -> NSString {
        let numberFormatter = NumberFormatter()
        numberFormatter.positiveFormat = "￥###,##0.00;"
        let formattedNumberString : NSString = numberFormatter.string(from: NSDecimalNumber.init(string: self as String))! as NSString
        return formattedNumberString
    }
    
    
    func UDKey(selectName:NSString) -> NSString {
        let selectNameStr = NSStringFromSelector(NSSelectorFromString(selectName as String)) as NSString
        return selectNameStr.appending("_forUserInfoKey") as NSString
    }
    
    func EncryptUDKey(selectName:NSString) -> NSString {
        let selectNameStr = NSStringFromSelector(NSSelectorFromString(selectName as String)) as NSString
        return selectNameStr.appending("_UZeExsKfBIwYCchE") as NSString
    }
    
}
